--[[ Lambda Actionbar
     Part of LambdaUI made by Gaius
	 Graphics are made by Gaius
	 ------------------------------
	 Contact me in irc://phcnet.de/#wow-interface
--]]

local filePath = "Interface\\Addons\\LambdaUI\\"
local genericFilePath = "Interface\\AddOns\\LambdaUI\\Generic\\"
local customized = false

--[[Lambda ActionBarButton Prototype
--]]
local LambdaButton = {}
function LambdaButton:new(positionX, positionY, Button, Owner)
	local LambdaButton = {}
	local LambdaButtonChildrens = {}
	
	LambdaButton = Button
	
	LambdaButton:ClearAllPoints()
	
	LambdaButton:SetWidth(32)
	LambdaButton:SetHeight(32)
	
	getglobal(LambdaButton:GetName().."Icon"):SetDrawLayer("BORDER")
		
	getglobal(LambdaButton:GetName().."Border"):SetTexture(nil)
	
	
	LambdaButton:SetPoint("TOPLEFT",Owner,"TOPLEFT",positionX, positionY)
	LambdaButton:SetNormalTexture(filePath.."ButtonBorder.tga")
	LambdaButton:SetPushedTexture(filePath.."ButtonBorder.tga")
	LambdaButton:SetDisabledTexture(filePath.."ButtonBorder.tga")
	
	LambdaButtonChildrens = {LambdaButton:GetRegions()}
	
	LambdaButtonChildrens[9]:Hide()
	LambdaButtonChildrens[8]:Hide()
	LambdaButtonChildrens[7]:Hide()
	LambdaButtonChildrens[6]:Hide()
	
	LambdaButton:SetParent(UIParent)
	
	return LambdaButton
end

--[[  Lambda ActionBar Prototype

--]]
local Lambda = {}

function Lambda:new()
	local Lambda = {}
    local filePath = filePath.."ActionBar\\"
	
	Lambda = CreateFrame("Frame", "LambdaActionbar", UIParent)
	Lambda.Overlay = Lambda:CreateTexture("LambdaActionbarOverlay","OVERLAY")
	Lambda.Background = Lambda:CreateTexture("LambdaActionbarBackground","BACKGROUND")
	Lambda.Border = Lambda:CreateTexture("LambdaActionbarBorder","ARTWORK")
	Lambda.InfoOrb = Lambda:CreateTexture("LambdaActionbarOrb","BORDER")
	Lambda.InfoOrbText = Lambda:CreateFontString("LambdaActionBarOrbText","ARTWORK")
	
	Lambda:SetWidth(512)
	Lambda:SetHeight(64)
	Lambda:SetPoint("BOTTOMLEFT",UIParent,"BOTTOMLEFT", 24, 26)
		
	Lambda.Overlay:SetTexture(filePath.."Overlay.tga")
	Lambda.Overlay:SetPoint("TOPLEFT",Lambda)
	Lambda.Overlay:SetWidth(512)
	Lambda.Overlay:SetHeight(64)
		
	Lambda.Border:SetTexture(filePath.."Border.tga")
	Lambda.Border:SetPoint("TOPLEFT",Lambda)
	Lambda.Border:SetWidth(512)
	Lambda.Border:SetHeight(64)
	
	Lambda.Background:SetTexture(filePath.."Background.tga")
	Lambda.Background:SetPoint("TOPLEFT",Lambda)
	Lambda.Background:SetWidth(512)
	Lambda.Background:SetHeight(64)
	
	Lambda.InfoOrb:SetTexture(filePath.."OrbFilling.tga")
	Lambda.InfoOrb:SetWidth(64)
	Lambda.InfoOrb:SetHeight(64)
	Lambda.InfoOrb:SetPoint("BOTTOMLEFT",Lambda,"BOTTOMLEFT",0, 0)
	
	Lambda.InfoOrbText:SetFont(genericFilePath.."bold.ttf", 12)
	Lambda.InfoOrbText:SetPoint("BOTTOMLEFT",Lambda.InfoOrb,"BOTTOMLEFT",0,0)
	Lambda.InfoOrbText:SetWidth(64)
	Lambda.InfoOrbText:SetHeight(64)
	
	
	Lambda.InfoOrbText:SetText("100%")
	
	Lambda.InfoOrb.OnEvent = function()
		if UnitMana("player") ~= 0 then
			local maxHeight = 64
			local Height = UnitMana("player") / UnitManaMax("player")
			local Cut = math.abs(1 - Height)
		
			Lambda.InfoOrb:SetTexCoord(0, 1, Cut, 1)
			Lambda.InfoOrb:SetHeight(maxHeight * Height)
			Lambda.InfoOrbText:SetFormattedText("%.1i%%",Height * 100)
		end
	end
	
	Lambda:SetScript("OnEvent",Lambda.InfoOrb.OnEvent)

	Lambda:RegisterEvent("UNIT_MANA")	
	Lambda:RegisterEvent("UNIT_FOCUS")	
	Lambda:RegisterEvent("UNIT_ENERGY")	
	Lambda:RegisterEvent("UNIT_HEALTH")	
	
	Lambda.UpdateFontColors = function()
		Lambda.InfoOrbText:SetVertexColor(unpack(LambdaUI.TextColors.OrbFont))
	end
	
	table.insert(LambdaObjectTable,Lambda:GetName())
	
	return Lambda
end 

local Kappa = {}

function Kappa:new()
	local Kappa = {}
    local filePath = filePath.."ActionBar\\"
	
	Kappa = CreateFrame("Frame", "LambdaBonusActionbar", UIParent)
	Kappa.Background = Kappa:CreateTexture("LambdaBonusActionbarBackground","BACKGROUND")
	Kappa.Border = Kappa:CreateTexture("LambdaBonusActionbarBorder","ARTWORK")
	Kappa.Overlay = Kappa:CreateTexture("LambdaBonusActionbarOverlay","OVERLAY")
	
	Kappa:SetWidth(512)
	Kappa:SetHeight(64)
	Kappa:SetPoint("BOTTOMLEFT",UIParent,"BOTTOMLEFT", 556, -5)
		
		
	Kappa.Border:SetTexture(filePath.."BonusActionBarBorder.tga")
	Kappa.Border:SetPoint("TOPLEFT",Kappa)
	Kappa.Border:SetWidth(512)
	Kappa.Border:SetHeight(64)
	
	Kappa.Background:SetTexture(filePath.."BonusActionBarBackground.tga")
	Kappa.Background:SetPoint("TOPLEFT",Kappa)
	Kappa.Background:SetWidth(512)
	Kappa.Background:SetHeight(32)
	
	Kappa.Overlay:SetTexture(filePath.."BonusActionBarOverlay.tga")
	Kappa.Overlay:SetPoint("TOPLEFT",Kappa)
	Kappa.Overlay:SetWidth(512)
	Kappa.Overlay:SetHeight(64)
	
	
	table.insert(LambdaObjectTable,Kappa:GetName())
	
	return Kappa
end

local Sigma = {}
function Sigma:new(iterator, from, to) -- actionbar skin
	local Sigma = {}
	local filePath = filePath.."ActionBar\\"
	
	Sigma = CreateFrame("Frame","LambdaActionBar"..from.."-"..to)
	Sigma:SetBackdrop({	bgFile = "Interface\\Buttons\\WHITE8x8",
		edgeFile = filePath.."ActionBarBorder.tga",
		tile = true,
		tileSize = 16,
		edgeSize = 18,
		insets = {
			left = 16,
			right = 16,
			top = 16,
			bottom = 16,
		}
	})
	
	Sigma:SetBackdropColor(unpack(LambdaUI.ThemeSettings.Border))
	Sigma:SetBackdropBorderColor(unpack(LambdaUI.ThemeSettings.Border))
	
	Sigma:SetParent(getglobal(iterator..from))
	
	Sigma:SetFrameLevel(0)
	
	Sigma:SetPoint("TOPLEFT",getglobal(iterator..from), "TOPLEFT", -16, 4)
	
	for i=from, to do
		local btn = getglobal(iterator..i)
			
		if btn then
			Sigma:SetPoint("BOTTOMRIGHT",getglobal(iterator..i), "BOTTOMRIGHT", 16, -4)
			btn:SetWidth(28)
			btn:SetHeight(28)
			btn:SetFrameLevel(2)
			btn.SetFrameLevel = function() end
			btn.border:SetAlpha(0)
			btn.normal:SetAlpha(0)
			local overlay = btn:CreateTexture(btn:GetName().."Overlay","OVERLAY")
			overlay:SetPoint("TOPLEFT",btn,"TOPLEFT",0,0)
			overlay:SetWidth(32)
			
			overlay:SetHeight(8)
			overlay:SetTexture(filePath.."singleGloss.tga")
			table.insert(LambdaObjectTable,btn:GetName())
		end
	end
	
	table.insert(LambdaObjectTable,Sigma:GetName())
	
	return Sigma
end

MainActionBar = Lambda:new()
BonusActionBar = Kappa:new()

function BT3Customize()
	
	for i=0,11 do
		local btn = getglobal("BT3Button"..i+1)
		btn:SetWidth(32)
		btn:SetHeight(32)
		btn:ClearAllPoints()
		btn.ClearAllPoints = function() end
		btn:SetPoint("TOPLEFT",MainActionBar,"TOPLEFT",73+(i*35), -31)
		btn:SetFrameLevel(1)
		getglobal(btn:GetName().."Icon"):SetDrawLayer("BORDER")
		
	end
	customized = true
end

function BongosCustomize()
	
	for i=0,11 do
		local btn = getglobal("BongosActionButton"..i+1)
		btn:SetWidth(32)
		btn:SetHeight(32)
		btn:ClearAllPoints()
		btn.ClearAllPoints = function() end
		btn:SetPoint("BOTTOMLEFT",MainActionBar,"BOTTOMLEFT",73+(i*35), 0)
		btn:SetFrameLevel(1)
				
		btn.normal:SetAlpha(0)
		btn.border:SetAlpha(0)
		btn.SetScale = function()
			btn:SetScale(MainActionBar:GetScale())
		end
		getglobal(btn:GetName().."Icon"):SetDrawLayer("BORDER")
		getglobal(btn:GetName().."Cooldown"):SetFrameLevel(3)
	end
	customized = true
end

function BonusBT3Customize()
	
	for i=12,23 do
		local btn = getglobal("BT3Button"..i+1)
		if btn then
			btn:SetWidth(32)
			btn:SetHeight(32)
			btn:ClearAllPoints()
			btn.ClearAllPoints = function() end
			btn:SetPoint("TOPLEFT",BonusActionBar,"TOPLEFT",14+((i-12)*33), 0)
			btn:SetFrameLevel(1)
			getglobal(btn:GetName().."Icon"):SetDrawLayer("BORDER")
		else
			BonusActionBar:Hide()
			break
		end
	end
	customized = true
end

function BonusBongosCustomize()
	local name = UnitName("player")
	local realm = GetRealmName()
	local profileKey = Bongos2DB.profileKeys[name .. " - "..realm]
	profile = Bongos2DB.profiles[profileKey]

	if profile.bars[2].hidden ~= true then
	
		for i=12,23 do
			local btn = getglobal("BongosActionButton"..i+1)
				btn:SetWidth(32)
				btn:SetHeight(32)
				btn:ClearAllPoints()
				btn.ClearAllPoints = function() end
				btn:SetPoint("TOPLEFT",BonusActionBar,"TOPLEFT",14+((i-12)*33), 0)
				btn:SetFrameLevel(1)
					
				btn.normal:SetAlpha(0)
				btn.border:SetAlpha(0)
			
				getglobal(btn:GetName().."Icon"):SetDrawLayer("BORDER")
				getglobal(btn:GetName().."Cooldown"):SetFrameLevel(3)
		end
		customized = true
	else
		BonusActionBar:Hide()
	end
end

MainActionBar:SetScript("OnUpdate", function()
	if not customized then
		if BT3Bar1 then
			BT3Customize()
			BonusBT3Customize()
		elseif BongosActionButton1 then
			BongosCustomize()
			BonusBongosCustomize()
			local Skin1 = Sigma:new("BongosActionButton", 25, 36)
			local Skin2 = Sigma:new("BongosActionButton", 37, 48)
			local Skin3 = Sigma:new("BongosActionButton", 49, 60)
			local Skin4 = Sigma:new("BongosActionButton", 61, 72)
			local Skin5 = Sigma:new("BongosActionButton", 73, 84)
			local Skin6 = Sigma:new("BongosActionButton", 85, 96)
			local Skin7 = Sigma:new("BongosActionButton", 97, 108)
			local Skin8 = Sigma:new("BongosActionButton", 109, 120)
			
		end
		MainActionBar:SetScript("OnUpdate", nil)
	end
end)
